package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.util.DesktopUtil;
	
	import flash.desktop.DockIcon;
	import flash.desktop.NativeApplication;
	import flash.desktop.NotificationType;
	import flash.desktop.SystemTrayIcon;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindow;
	import flash.events.Event;
	import flash.events.InvokeEvent;
	import flash.events.ScreenMouseEvent;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	
	public class RogoBoxMediator extends Mediator
	{
		public function RogoBoxMediator(viewComponent:UIComponent)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'MicroblogMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		[Embed(source="assets/16.png")]
		private var icon16:Class;
		
		[Embed(source="assets/32.png")]
		private var icon32:Class;
		
		[Embed(source="assets/48.png")]
		private var icon48:Class;
		
		[Embed(source="assets/128.png")]
		private var icon128:Class;
		
		//--------------------------------------------------------------------------
		//
		//  Methdos
		//
		//--------------------------------------------------------------------------
		
		public function get view():RogoBox
		{
			return viewComponent as RogoBox;
		}
		
		public function createTrayIcon():void
		{
			if(NativeApplication.supportsDockIcon)
			{
				// Mac os
				var dockIcon:DockIcon = DockIcon(NativeApplication.nativeApplication.icon);
				NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, app_invokeHandler);
				dockIcon.menu = createTrayMenu();
			}
			
			if(NativeApplication.supportsSystemTrayIcon)
			{
				// Windows
				var trayIcon:SystemTrayIcon = SystemTrayIcon(NativeApplication.nativeApplication.icon);
				trayIcon.addEventListener(ScreenMouseEvent.CLICK, trayIcon_clickHandler);
				trayIcon.menu = createTrayMenu();
				trayIcon.tooltip = 'RogosBox';
			}
		}
		
		public function createTrayMenu():NativeMenu
		{
			var trayMenu:NativeMenu = new NativeMenu();
			
			var openMain:NativeMenuItem = new NativeMenuItem('打开');
			openMain.addEventListener(Event.SELECT, openMain_selectHandler);
			trayMenu.addItem(openMain);
			
			var sep1:NativeMenuItem = new NativeMenuItem('sep1', true);
			trayMenu.addItem(sep1);
			
			var update:NativeMenuItem = new NativeMenuItem('更新');
			update.addEventListener(Event.SELECT, update_selectHandler);
			trayMenu.addItem(update);
			
			var sep2:NativeMenuItem = new NativeMenuItem('sep2', true);
			trayMenu.addItem(sep2);
			
			var exit:NativeMenuItem = new NativeMenuItem('退出');
			exit.addEventListener(Event.SELECT, exit_selectHandler);
			trayMenu.addItem(exit);
			
			return trayMenu;
		}
		
		private function dock():void
		{
			view.stage.nativeWindow.visible = false;
			NativeApplication.nativeApplication.icon.bitmaps = [new icon16(), 
				new icon32(), new icon48(), new icon128()];
		}
		
		private function undock():void
		{
			view.stage.nativeWindow.visible = true;
			NativeApplication.nativeApplication.icon.bitmaps = [];
			view.stage.nativeWindow.activate();
		}
		
		private function notifyApp():void
		{
			if(NativeApplication.supportsDockIcon)
			{
				var dock:DockIcon = NativeApplication.nativeApplication.icon as DockIcon;
				dock.bounce(NotificationType.INFORMATIONAL);
			} 
			else if (NativeApplication.supportsSystemTrayIcon)
			{
				view.stage.nativeWindow.notifyUser(NotificationType.INFORMATIONAL);
			}
		}
		
		private function closeApp():void
		{
			for each (var window:NativeWindow in view.nativeApplication.openedWindows)
			{
				window.close();
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methdos
		//
		//--------------------------------------------------------------------------
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.LOGIN_RESULT,
				AppFacade.LOGIN_FAIL,
				AppFacade.LOADING_START,
				AppFacade.LOADING_END,
				AppFacade.NOTIFICATION,
				AppFacade.SET_APP_ENABLE
			];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case AppFacade.LOGIN_RESULT:
					view.currentState = 'login';
					break;
				case AppFacade.LOADING_START:
					view.loadingBar.visible = true;
					break;
				case AppFacade.LOADING_END:
					view.loadingBar.visible = false;
					break;
				case AppFacade.NOTIFICATION:
					notifyApp();
					break;
				case AppFacade.SET_APP_ENABLE:
					FlexGlobals.topLevelApplication.enabled = notification.param as Boolean;
					break;
				case AppFacade.LOGIN_FAIL:
					sendNotification(AppFacade.SHOW_TIP, '登录失败');
					break;
			}
		}
		
		override public function onRegister():void
		{
			DesktopUtil.moveToCenter(view.nativeWindow);
			
			view.addEventListener(Event.CLOSING, view_closingHandler);
			
			// 程序更新
			sendNotification(AppFacade.UPDATE_APP, false);
			
			createTrayIcon();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function view_closingHandler(e:Event):void
		{
			e.preventDefault();
			dock();
		}
		
		private function openMain_selectHandler(e:Event):void
		{
			undock();
		}
		
		private function exit_selectHandler(e:Event):void
		{
			closeApp();
		}
		
		private function app_invokeHandler(e:InvokeEvent):void
		{
			undock();
		}
		
		private function trayIcon_clickHandler(e:ScreenMouseEvent):void
		{
			undock();
		}
		
		private function update_selectHandler(e:Event):void
		{
			sendNotification(AppFacade.UPDATE_APP, true);
		}
		
	}
}